var vm = new Vue({
    el: "#main",
    data: {
        // 遮罩层
        loading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 定时任务表格数据
        jobList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示详细弹出层
        openView: false,
        // 任务组名字典
        jobGroupOptions: [],
        // 状态字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          jobName: undefined,
          jobGroup: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          jobName: [
            { required: true, message: "任务名称不能为空", trigger: "blur" }
          ],
          invokeTarget: [
            { required: true, message: "调用目标字符串不能为空", trigger: "blur" }
          ],
          cronExpression: [
            { required: true, message: "cron执行表达式不能为空", trigger: "blur" }
          ]
        },
        multipleSelection: [],
        selectSingle: {}
        
        
    },
    created: function() {
         this.getList();
    },
    methods: {
        toggleSelection(rows) {
            if (rows) {
              rows.forEach(row => {
                this.$refs.multipleTable.toggleRowSelection(row);
              });
            } else {
              this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
           /** 查询定时任务列表 */
        getList() {
            this.loading = true;
            axios({
				method: 'post',
				url: App.envUrl.backtrackUrl + "monitor/job/list",
				data: this.queryParams,
			}).then(response => {
            	this.jobList = response.data.rows;
            	this.total = response.data.total;
            	this.loading = false;
            });
        },
        // 任务组名字典翻译
        jobGroupFormat(row, column) {
            // return this.selectDictLabel(this.jobGroupOptions, row.jobGroup);
        },
        // 状态字典翻译
        statusFormat(row, column) {
            // return this.selectDictLabel(this.statusOptions, row.status);
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
	            jobId: undefined,
	            jobName: undefined,
	            jobGroup: undefined,
	            invokeTarget: undefined,
	            cronExpression: undefined,
	            misfirePolicy: 1,
	            concurrent: 1,
	            status: "0"
            };
            // this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            // this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.jobId);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        // 任务状态修改
        handleStatusChange(row) {
            let text = row.status === "0" ? "启用" : "停用";
            this.$confirm('确认要"' + text + '""' + row.jobName + '"任务吗?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return axios({
    				method: 'post',
    				url: App.envUrl.backtrackUrl + "monitor/job/changeStatus?jobId="+row.jobId+"&status="+row.status,
    			});
            }).then(() => {
            	this.getList();
                this.$message({
                    message: text + "成功",
                    type: 'success'
            	});
            }).catch(function() {
                row.status = row.status === "0" ? "1" : "0";
            });
        },
        /* 立即执行一次 */
        handleRun(row) {
            this.$confirm('确认要立即执行一次"' + row.jobName + '"任务吗?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return axios({
    				method: 'post',
    				url: App.envUrl.backtrackUrl + "monitor/job/run?jobId="+row.jobId+"&jobGroup="+row.jobGroup,
                });
            }).then(() => {
            	this.$message({
                    message: '执行成功',
                    type: 'success'
            	});
            }).catch(function() {
            	this.$message({
                    message: '执行失败',
                    type: 'warning'
            	});
            });
        },
        /** 任务详细信息 */
        handleView(row) {
        	axios({
				method: 'post',
				url: App.envUrl.backtrackUrl + "monitor/job/detail?jobId="+row.jobId,
			}).then(response => {
	            this.form = response.data;
	            this.openView = true;
            });
        },
        /** 任务日志列表查询 */
        handleJobLog() {
            this.$router.push("/job/log");
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加任务";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const jobId = row.jobId || this.ids;
            axios({
				method: 'post',
				url: App.envUrl.backtrackUrl + "monitor/job/detail?jobId="+jobId,
			}).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改任务";
            });
        },
        /** 提交按钮 */
        submitForm: function() {
            this.$refs["form"].validate(valid => {
            if (valid) {
                if (this.form.jobId != undefined) {
                	axios({
        				method: 'post',
        				url: App.envUrl.backtrackUrl + "monitor/job/edit",
        				data: {
    						jobId: this.form.jobId,
    						concurrent: this.form.concurrent,
    						cronExpression: this.form.cronExpression,
    						invokeTarget: this.form.invokeTarget,
    						jobName: this.form.jobName,
    						misfirePolicy: this.form.misfirePolicy,
    						status: this.form.status,
        				},
        			}).then(response => {
	                    if (response.data.code === 0) {
	                    	this.$message({
	                            message: '修改成功',
	                            type: 'success'
	                    	});
		                    this.open = false;
		                    this.getList();
	                    }else{
	                    	this.$message({
	                            message: response.data.msg,
	                            type: 'warning'
	                    	});
	                    }
	                });
                } else {
                	axios({
        				method: 'post',
        				url: App.envUrl.backtrackUrl + "monitor/job/add",
        				data: this.form,
        			}).then(response => {
	                    if (response.data.code === 0) {
	                    	this.$message({
	                            message: '新增成功',
	                            type: 'success'
	                    	});
		                    this.open = false;
		                    this.getList();
	                    }else{
	                    	this.$message({
	                            message: response.data.msg,
	                            type: 'warning'
	                    	});
	                    }
	                });
                }
            }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const jobIds = row.jobId || this.ids;
            this.$confirm('是否确认删除定时任务编号为"' + jobIds + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return axios({
    				method: 'post',
    				url: App.envUrl.backtrackUrl + "monitor/job/remove?ids="+jobIds.join(','),
    			});
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(function() {
            	this.$message({
                    message: '删除失败',
                    type: 'warning'
            	});
            });
        },
    }
    
})  


